package com.lwx.sys.realm;

import com.lwx.sys.constant.Constant;
import com.lwx.sys.domain.User;
import com.lwx.sys.service.PermissionService;
import com.lwx.sys.service.RoleService;
import com.lwx.sys.service.UserService;
import com.lwx.sys.utils.ActiveUser;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    /**
     * 认证-登录
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取前台用户名-身份信息
        String username = authenticationToken.getPrincipal().toString();
        //根据用户名查数据库
        User user =  userService.queryUserByUserName(username);
        if(ObjectUtils.isNotEmpty(user)){
            ActiveUser activeUser = new ActiveUser();
            activeUser.setUser(user);
            //普通用户
            if(user.getType().equals(Constant.USER_TYPE_NORMAL)){
                //查询该用户所有角色
                List<String> roles=roleService.queryRoleNamesByUserId(user.getId());
                activeUser.setRoles(roles);
                //查询该用户所有的权限
                List<String> permissions=permissionService.queryPermissionByUserId(user.getId());
                activeUser.setPermissions(permissions);
            }
            //md5易破解，加点盐
            //1.获取盐
            String salt = user.getSalt();
            //2.转化
            ByteSource byteSource = ByteSource.Util.bytes(salt);
            //密码比对
            return new SimpleAuthenticationInfo(activeUser, user.getPwd(), byteSource, getName());
        }
        return null;
    }

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获取登录用户
        ActiveUser activeUser = (ActiveUser) principalCollection.getPrimaryPrincipal();
        if(activeUser.getUser().getType().equals(Constant.USER_TYPE_SUPER)) {
            //超级用户
            info.addStringPermission("*:*");//拥有所有权限
        }else {
            //普通用户
            //把该用户的所有的角色放入到shiro
            info.addRoles(activeUser.getRoles());
            //把该用户的所有的权限放入到shiro
            info.addStringPermissions(activeUser.getPermissions());
        }
        return info;
    }
}
